//
// Created by 33145 on 2023/5/27.
//

#include "config.h"
#include <tinyxml/tinyxml.h>

#define READ_XML_NODE(name, parent) \
TiXmlElement* name##_node = parent->FirstChildElement(#name); \
if (!name##_node) { \
  printf("Start rocket server error, failed to onRead node [%s]\n", #name); \
  exit(0); \
} \



#define READ_STR_FROM_XML_NODE(name, parent) \
  TiXmlElement* name##_node = parent->FirstChildElement(#name); \
  if (!name##_node|| !name##_node->GetText()) { \
    printf("Start rocket server error, failed to onRead config file %s\n", #name); \
    exit(0); \
  } \
  std::string name##_str = std::string(name##_node->GetText()); \

namespace rocket {

  static Config* g_config = nullptr;


  Config* Config::GetGlobalConfig() {
    return g_config;
  }

  void Config::SetGlobalConfig(const char* xmlfile) {
    if (g_config == NULL) {
      if (xmlfile != NULL) {
        g_config = new Config(xmlfile);
      } else {
        g_config = new Config();
      }
    }
  }

  Config::Config() {
   _log_level = "DEBUG";
  }

  Config::Config(const char* xmlfile) {
    TiXmlDocument* xml_document = new TiXmlDocument();

    bool rt = xml_document->LoadFile(xmlfile);
    if (!rt) {
      printf("Start rocket server error, failed to onRead config file %s, error info[%s] \n", xmlfile, xml_document->ErrorDesc());
      exit(0);
    }
    // 读出root、log、server
    READ_XML_NODE(root, xml_document);
    READ_XML_NODE(log, root_node);
    READ_XML_NODE(server, root_node);

    // log_level_str log_file_name_str...
    READ_STR_FROM_XML_NODE(log_level, log_node);
    READ_STR_FROM_XML_NODE(log_file_name, log_node);
    READ_STR_FROM_XML_NODE(log_file_path, log_node);
    READ_STR_FROM_XML_NODE(log_max_file_size, log_node);
    READ_STR_FROM_XML_NODE(log_sync_interval, log_node);

   _log_level = log_level_str;
   _log_file_name = log_file_name_str;
   _log_file_path = log_file_path_str;
   _log_max_file_size = std::atoi(log_max_file_size_str.c_str());
   _log_sync_inteval = std::atoi(log_sync_interval_str.c_str());

    printf("LOG -- CONFIG LEVEL[%s], FILE_NAME[%s],FILE_PATH[%s] MAX_FILE_SIZE[%d B], SYNC_INTEVAL[%d ms]\n",
          _log_level.c_str(),_log_file_name.c_str(),_log_file_path.c_str(),_log_max_file_size,_log_sync_inteval);

    READ_STR_FROM_XML_NODE(port, server_node);
    READ_STR_FROM_XML_NODE(io_threads, server_node);

   _port = std::atoi(port_str.c_str());
   _io_threads = std::atoi(io_threads_str.c_str());

    printf("Server -- PORT[%d], IO Threads[%d]\n",_port,_io_threads);
  }

}